App({
  http(method, url, data, success, fail) {
    let _data = data || {};
    let _success = success
    let _header = {
      'content-type': 'application/json'
    };
    if (wx.getStorageSync('token')){
      _header['token'] = wx.getStorageSync('token')
    }
    let _fail = fail || function (e) {
      console.log(e)
    };
    if (arguments.length == 3 && typeof _data == 'function') {
      _success = _data
      _data = {}
    } 
    if(_data.id){
      url = url + _data.id + '/'
      delete _data.id 
    }
    console.log(url,method,_header,_data);
    wx.request({
      url: url,
      method: method,
      header: _header,
      data: _data,
      success: function (res) {
        if (res.statusCode != 404 && res.statusCode != 500 && res.statusCode != 400) {
           if (res.data.code != 100) {
            wx.showToast({
              title: res.data.msg + '',
              icon: 'none'
            })
          }else{
            _success(res.data);
          }
        } else {
          wx.showToast({
            title: '接口  错误 ' + res.statusCode,
            icon: 'none'
          })
        }
      },
      fail: function (res) {
        console.log(`======== 接口  请求失败 ========`);
        if (typeof _fail == 'function') {
          _fail(res);
        }
      }
    });
  },
  formatMsgTime(timespan) {
    var dateTime = new Date(timespan);
    var year = dateTime.getFullYear();
    var month = dateTime.getMonth() + 1;
    var day = dateTime.getDate();
    var hour = dateTime.getHours();
    var minute = dateTime.getMinutes();
    var second = dateTime.getSeconds();
    var now = new Date();
    var now_new = now.getTime(); //typescript转换写法

    var milliseconds = 0;
    var timeSpanStr;

    milliseconds = now_new - dateTime.getTime();

    if (milliseconds <= 1000 * 60 * 1) {
      timeSpanStr = '刚刚';
    } else if (1000 * 60 * 1 < milliseconds && milliseconds <= 1000 * 60 * 60) {
      timeSpanStr = Math.round((milliseconds / (1000 * 60))) + '分钟前';
    } else if (1000 * 60 * 60 * 1 < milliseconds && milliseconds <= 1000 * 60 * 60 * 24) {
      timeSpanStr = Math.round(milliseconds / (1000 * 60 * 60)) + '小时前';
    } else if (1000 * 60 * 60 * 24 < milliseconds && milliseconds <= 1000 * 60 * 60 * 24 * 15) {
      timeSpanStr = Math.round(milliseconds / (1000 * 60 * 60 * 24)) + '天前';
    } else if (milliseconds > 1000 * 60 * 60 * 24 * 15 && year == now.getFullYear()) {
      timeSpanStr = month + '-' + day + ' ' + hour + ':' + minute;
    } else {
      timeSpanStr = timespan;
    }
    return timeSpanStr;
  },
})
